<?php

namespace app\ljhs\controller;
use app\common\controller\Ljhs;
use app\admin\model\AdminLog;
use app\common\model\RkComfj;
use app\common\model\RkQtfj;
use fast\Random;
use think\Config;
use think\Hook;
use think\Validate;
/**
 * 站长接口
 */
class Stand extends Ljhs
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    /**
     * 报销
     * @return void
     */
    public function baoxiao()
    {
        $user = $this->auth->getUserInfo();
        $params = $this->request->post();
        $params['stand_id'] = $user['stand_id'];
        if($params['type'] == 3){
            $params['user_id'] = $user['id'];
        }
        $res = \app\common\model\Baoxiao::create($params);
        if($res){
//            \app\common\model\Finance::addData($params, 2);
            $this->success(__('提交成功'),$res);
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 获取产品
     * @return void
     */
    public function getProduct()
    {
        $type = $this->request->post('type');
        $where  = [];
        if($type){
            $where['type'] = $type;
        }
        $user = $this->auth->getUserInfo();
        $where['stand_id'] = $user['stand_id'];
        $list = \app\common\model\Cgproduct::where($where)->order('id desc')->select();
        $this->success(__('获取成功'),$list);
    }

    /**
     * 添加产品
     * @return void
     */
    public function addProduct()
    {
       $type = $this->request->post('type');
       $name = $this->request->post('name');
       if(!$name || !$type){
           $this->error(__('参数错误'));
       }
       $user = $this->auth->getUserInfo();
       $res = \app\common\model\Cgproduct::create(['name'=>$name,'type'=>$type,'stand_id'=>$user['stand_id']]);
       if($res){
           $this->success(__('添加成功'),$res);
       }else{
           $this->error(__('添加失败'));
       }
    }

    /**
     * 采购入库
     * @return void
     */
    public function cgrk()
    {
        $user = $this->auth->getUserInfo();
        $params = $this->request->post();
        if(!isset($params['cgproduct_id']) || !isset($params['num']) || !isset($params['price'])){
            $this->error(__('参数错误'));
        }
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $res = \app\common\model\Cgrk::create($params);
        if($res){
            //更新此产品的库存
            $cgproduct = \app\common\model\Cgproduct::get($params['cgproduct_id']);
            $cgproduct->all_num = $cgproduct->all_num + $params['num'];
            $cgproduct->save();
            $this->success(__('提交成功'),$res);
        }else{
            $this->error(__('提交失败'));
        }
    }


    /**
     * 入库单-企业自送
     * @return void
     */
    public function rkComfj()
    {
        $com_id = $this->request->post('com_id');
        if(!$com_id){
            $this->error(__('请选择企业'));
        }
        $kg = $this->request->post('kg');
        if(!$kg){
            $this->error(__('请选择车次时间'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $params = $this->request->post();
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['type'] = 2;
        $params['car_no'] = $car_no;
        $params['ratio'] = 100;
        $params['status'] = 1;
        $params['com_kg'] = $kg;
        $params['car_time'] = time();
        $res = RkComfj::create($params);
        if($res){
            $this->success(__('提交成功'),$res);
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 入库单-其他入库
     * @return void
     */
    public function rkQtFj()
    {
        $com_name = $this->request->post('com_name');
        if(!$com_name){
            $this->error(__('请输入企业信息'));
        }
//        $cat_time = time();
        $price = $this->request->post('price');
        $unit_price = $this->request->post('unit_price');
        $pay_type = $this->request->post('pay_type');
        if(!$price || !$pay_type || !$unit_price){
            $this->error(__('参数错误'));
        }
        $kg = $this->request->post('kg');
        if(!$kg){
            $this->error(__('请输入重量'));
        }
        $user = $this->auth->getUserInfo();
        $firstChar = chr(mt_rand(ord('A'), ord('Z')));
        $car_no = $firstChar.mt_rand(100000, 999999);//车次编号
        $dataAll = [
            'user_id'=> $user['id'],
            'com_name' => $com_name,
            'price' => $price,
            'pay_type' => $pay_type,
            'car_no' => $car_no,
            'car_time' => time(),
            'ratio'=>100,
            'stand_id' => $user['stand_id'],
            'type'=>2,
            'kg' => $kg,
            'status'=>2,
            'com_kg' => $kg,
            'unit_price'=>$unit_price,
            'createtime' => time()
        ];

        $res = RkQtfj::create($dataAll);
        if($res){
            $this->success(__('提交成功'),$res);
        }else{
            $this->error(__('提交失败'));
        }
    }



    /**
     * 成品出库
     * @return void
     */
    public function cpck()
    {
        $num = $this->request->post('num');
        if(!$num){
            $this->error(__('参数错误'));
        }
        $user = $this->auth->getUserInfo();
        $params = $this->request->post();
//        if(!isset($params['kg'])){
//            $this->error(__('请输入吨数'));
//        }
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = \app\common\model\Cpck::create($params);
        if($res){
            //更新此站点的库存
            $stand = \app\common\model\Stand::get($user['stand_id']);
            $stand->stock = $stand->stock - $num;
            $stand->ck_num = $stand->ck_num + $num;
//            $stand->ck_kg = $stand->ck_kg + $params['kg'];
            $stand->save();
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }


    /**
     * 获取驾驶员入库单信息
     * @return void
     */
    public function getRkList()
    {
        $type = $this->request->post('type');
        $user = $this->auth->getUserInfo();
        if($type == 1){//企业到分拣入库
            $list = \app\common\model\RkComfj::where('stand_id',$user['stand_id'])
                ->group('car_no')
                ->paginate()->each(function ($item){
                $item['admin'] = \app\common\model\Admin::field('nickname,device_no')->find($item['user_id']);
                return $item;
            });
        }
        if($type == 2){//其他到分拣入库
            $list = \app\common\model\RkQtfj::where('stand_id',$user['stand_id'])->paginate()->each(function ($item){
                $item['admin'] = \app\common\model\Admin::field('nickname,device_no')->find($item['user_id']);
                return $item;
            });
        }
        if($type == 3){//秸秆入库
            $list = \app\common\model\RkJg::where('stand_id',$user['stand_id'])->paginate()->each(function ($item){
                $item['admin'] = \app\common\model\Admin::field('nickname,device_no')->find($item['user_id']);
                return $item;
            });
        }
        $this->success(__('获取成功'),$list);
    }

    /**
     * 添加入库单库存
     * @return void
     */
    public function addKg()
    {
        $type = $this->request->post('type');
        $car_no = $this->request->post('car_no');
        $kg = $this->request->post('kg');
        if(!$kg){
            $this->error(__('请输入重量'));
        }
        $user = $this->auth->getUserInfo();
        $res = [];
        if($type == 1){//企业到分拣入库
            $info = \app\common\model\RkComfj::where('car_no',$car_no)->field('id,stand_id,ratio')->select();
            if($info){
                foreach ($info as $k=>$v){
                    $update = [];
                    $update['kg'] = $kg * ($v['ratio']/100);
                    $update['com_kg'] = $kg * ($v['ratio']/100);
                    $update['status'] = 1;
                    $res = \app\common\model\RkComfj::update($update,['id'=>$v['id']]);
                }
            }
        }elseif ($type == 2){
            $info = \app\common\model\RkQtfj::where('car_no',$car_no)->field('id,ratio')->select();
            if($info){
                foreach ($info as $k=>$v){
                    $update = [];
                    $update['kg'] = $kg * ($v['ratio']/100);
                    $update['com_kg'] = $kg * ($v['ratio']/100);
                    $update['status'] = 1;
                    $res = \app\common\model\RkQtfj::update($update,['id'=>$v['id']]);
                }
            }
        }elseif ($type == 3){
            $info = \app\common\model\RkJg::where('car_no',$car_no)->field('id,ratio')->select();
            if($info){
                foreach ($info as $k=>$v){
                    $update = [];
                    $update['kg'] = $kg * ($v['ratio']/100);
                    $update['com_kg'] = $kg * ($v['ratio']/100);
                    $update['status'] = 1;
                    $res = \app\common\model\RkJg::update($update,['id'=>$v['id']]);
                }
            }
        }
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 站长安全检测
     * @return void
     */
    public function secureCheck()
    {
        $params = $this->request->post();
        if(!isset($params['xfs_status'])){
            $this->error(__('参数错误'));
        }
        $user = $this->auth->getUserInfo();
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = \app\common\model\SecureCheck::create($params);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }

    /**
     * 站长卫生检测
     * @return void
     */
    public function sceneCheck()
    {
        $params = $this->request->post();
        if(!isset($params['fjq_status'])){
            $this->error(__('参数错误'));
        }
        $user = $this->auth->getUserInfo();
        $params['user_id'] = $user['id'];
        $params['stand_id'] = $user['stand_id'];
        $params['createtime'] = time();
        $res = \app\common\model\SceneCheck::create($params);
        if($res){
            $this->success(__('提交成功'));
        }else{
            $this->error(__('提交失败'));
        }
    }






}
